package com.ff.javamonkey.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import com.ff.javamonkey.socket.message.Message;
import com.ff.javamonkey.util.ConnectionSettings;

public class SocketClientConnect implements ClientConnect {
	// Debugging
	private final String TAG = "SocketClientConnect";
	// General variables
	private boolean connected = false;
	// Socket variable
	private Socket sock;

	public SocketClientConnect() {
		sock = new Socket();
	}
	
	public void connect(String host, String port) throws IOException {
		InetSocketAddress inetSock = new InetSocketAddress(ConnectionSettings.FDCU_PARENT_HOST,
				ConnectionSettings.FDCU_PARENT_PORT);
		sock.connect(inetSock);
		setConnected(true);
	}
	
	public void connect(String host, int port) throws IOException {
		InetSocketAddress inetSock = new InetSocketAddress(host, port);
		sock.connect(inetSock);
		setConnected(true);
	}

	public void close() throws IOException {
		// TODO Auto-generated method stub
		sock.close();
	}

	public int getFileSize(String fileName) throws IOException {
		// TODO Auto-generated method stub
		return 0;
	}

	public byte[] getFile(String fileName) throws IOException {
		// TODO Auto-generated method stub
		return null;
	}

	public String getMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	public void setConnected(boolean conn) {
		this.connected = conn;
	}
	
	public boolean isConnected() {
		return connected;
	}

	public int getConnectionStatus() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setConnectionStatus(int stat) {
		// TODO Auto-generated method stub

	}

	public int getReadStatus() {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setReadStatus(int stat) {
		// TODO Auto-generated method stub

	}

	/**
	 * It is assumed that the socket is connected before calling this method
	 */
	public void write(Message message) {
		try {
			DataOutputStream writer = new DataOutputStream(sock.getOutputStream());
			// Please NOTE that this implementation is *still* using signed short. The correct implementation would be using an unsigned short.
			writer.writeShort(message.getMessageLength());
			writer.writeBytes(String.valueOf(message.getCommand())+String.valueOf(message.getEncodedMessage()));
			writer.flush();
			// New algorithm written by Alex Sandra
			// short tmpint = (short) ((message.getMessageLength() & 0xFF) << 8);
			// byte[] tmpx = ByteBuffer.allocate(2).putShort(tmpint).array();
			// ByteArrayOutputStream tmpb = new ByteArrayOutputStream();
			// tmpb.write(tmpx);
			// tmpb.write(message.getCommand());
			// tmpb.write(message.encodestr.getBytes());
			// sock.setSendBufferSize(tmpb.size());
			// writer.write(tmpb.toByteArray(),0,tmpb.size());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * It is assumed that the socket is connected before calling this method
	 */
	public String read() {
		try {
			DataInputStream reader = new DataInputStream(sock.getInputStream());
//			int length = reader.readUnsignedShort();
//			int b = reader.readUnsignedByte();
			StringBuffer buf = new StringBuffer();
			byte[] buffer = new byte[]{};
			reader.read(buffer);
//			buf.append(Message.decodeCommand(b)).append(" - ");
//			for (int i = 0; i < length; i++) {
				//buf.append(Byte.toString(reader.readByte()));
				//buf.append(reader.readChar());
//				buf.append(Message.decodeByte(reader.readByte()));
//			}
			for (byte b: buffer) {
				buf.append(b);
			}
			return buf.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public File getFile(String fileName, String fileOut) throws IOException {
		// TODO Auto-generated method stub
		return null;
	}

	public String[] getFileNames(String extension, String directory) {
		// TODO Auto-generated method stub
		return null;
	}

	public void write(File file) {
		// TODO Auto-generated method stub
		
	}
}
